//
//  LoginView.m
//  YTAutoLayout
//
//  Created by fly on 14/12/12.
//  Copyright (c) 2014年 fly. All rights reserved.
//

#import "LoginView.h"
#import "Constant.h"
#import "EnvironmentVariable.h"

@interface LoginView ()

@property(nonatomic ,weak) UIButton* btnLogin;

@end

@implementation LoginView{
    UIImageView *_logoImageView;
    UIImageView *_titleImageView;
    UIImageView *_settingImageView;
}

-(instancetype)init{
    if(self= [super init])
    {
        [self createLoginView];
    }
    return self;
}

-(void)createLoginView{
    UIFont *smallFont = [UIFont systemFontOfSize:13];
    self.backgroundColor = [UIColor whiteColor];
    
    //1.创建控件
    UIView *cView =[[UIView alloc] init];
    //1.1六个lable
    UILabel *labName = [[UILabel alloc] init];
    labName.text = @"用户";
    labName.textColor = [UIColor blackColor];
    
    UILabel *labPwd = [[UILabel alloc] init];
    labPwd.text = @"密码";
    labPwd.textColor = [UIColor blackColor];
    
    UILabel *labSavePwd = [[UILabel alloc] init];
    labSavePwd.text = @"记住密码";
    labSavePwd.hidden = YES;
    labSavePwd.font = smallFont;
    labSavePwd.textColor = [UIColor blackColor];
    
    UILabel *labAuto = [[UILabel alloc] init];
    labAuto.text = @"自动登录";
    labAuto.hidden = YES;
    labAuto.font = smallFont;
    labAuto.textColor = [UIColor blackColor];
    
    UIButton *btnLostPwd = [[UIButton alloc] init];
    [btnLostPwd setTitle:@"" forState:UIControlStateNormal];
    btnLostPwd.titleLabel.font = smallFont;
    //btnLostPwd.titleLabel.textColor  = [UIColor blackColor];
    [btnLostPwd setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btnLostPwd setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    //1.2 两个textfiled
    UITextField *textName = [[UITextField alloc] init];
    textName.placeholder = @"请输入用户名";
    textName.borderStyle = UITextBorderStyleRoundedRect;
    UIImageView *imageViewName = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_btn_username"]];
    imageViewName.frame=CGRectMake(0, 0, 40, 40);
    textName.leftView = imageViewName;
    textName.leftViewMode=UITextFieldViewModeAlways;
    _textName = textName;
    
    UITextField *textPwd = [[UITextField alloc] init];
    textPwd.placeholder = @"请输入密码";
    textPwd.borderStyle = UITextBorderStyleRoundedRect;
    textPwd.secureTextEntry = YES;
    UIImageView *imageViewPwd = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_btn_password"]];
    imageViewPwd.frame=CGRectMake(0, 0, 40, 40);
    textPwd.leftView = imageViewPwd;
    textPwd.leftViewMode=UITextFieldViewModeAlways;
    textPwd.clearButtonMode=UITextFieldViewModeWhileEditing;
    _textPwd = textPwd;
    
    //1.3两个switch
    UISwitch *swtSave = [[UISwitch alloc] init];
    self.savePwd = swtSave;
    [swtSave setOn:YES];
    
    UISwitch *swtAuto = [[UISwitch alloc] init];
    [swtAuto setOn:NO];
    self.autoLogin = swtAuto;
    
    self.savePwd.hidden = YES;
    self.autoLogin.hidden = YES;
    
    //1.4 一个UIbutton
    UIButton *btnLogin = [[UIButton alloc] init];
    [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    [btnLogin  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnLogin.layer.cornerRadius = 5.0;
    [btnLogin setBackgroundColor:EF_OLD_RED_COLOR];
    self.btnLogin = btnLogin;
    
    
    //2.将控件添加到view将控件的tras  属性设置为no
    [cView addSubview:labAuto];
    [cView addSubview:btnLostPwd];
    [cView addSubview:labName];
    [cView addSubview:labPwd];
    [cView addSubview:labSavePwd];
    
    [cView addSubview:textPwd];
    [cView addSubview:textName];
    
    [cView addSubview:swtAuto];
    [cView addSubview:swtSave];
    
    [cView addSubview:btnLogin];
    
    [self addSubview:cView];
    
    for (UIView *itemView in cView.subviews) {
        itemView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    cView.translatesAutoresizingMaskIntoConstraints = NO;
    //4.添加约束条件
    
    //cView添加约束
    NSMutableArray *cViewConstraints = [[NSMutableArray alloc] init];
    
    //水平约束
    [cViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[cView]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(cView)]];
    //垂直约束
    [cViewConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-150-[cView]-100-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(cView)]];
    //4.1 给cView 添加约束条件
    [self addConstraints:cViewConstraints];
    
    cViewConstraints = nil;
    
#pragma mark - //4.2用户名约束
    
    NSMutableArray *nameConstraints = [[NSMutableArray alloc] init];
    //水平约束
    [nameConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[labName(==0)]-0-[textName(>=100)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labName,textName)]];
    
    //垂直约束
    [nameConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[labName(==40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labName)]];
    
    
    [nameConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[textName(labName)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(textName,labName)]];
    [cView addConstraints:nameConstraints];
    
    nameConstraints = nil;
    
#pragma mark -//4.3密码约束
    
    NSMutableArray *pwdConstraints = [[NSMutableArray alloc] init];
    //水平约束
    [pwdConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[labPwd(==0)]-0-[textPwd(>=100)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labPwd,textPwd)]];
    
    //垂直约束
    [pwdConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[labName]-20-[labPwd(==40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labName,labPwd)]];
    
    
    [pwdConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[textName]-20-[textPwd(labPwd)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(textName,textPwd,labPwd)]];
    [cView addConstraints:pwdConstraints];
    pwdConstraints = nil;
    
    
#pragma mark -//4.4登录按钮约束
    NSMutableArray *loginConstraints = [[NSMutableArray alloc] init];
    //水平约束
    [loginConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[btnLogin]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnLogin)]];
    
    //垂直约束
    [loginConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[labPwd]-20-[btnLogin(==44)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labPwd,btnLogin)]];
    
    [cView addConstraints:loginConstraints];
    loginConstraints = nil;
    
#pragma mark -//4.5记住密码自动登录约束
    NSMutableArray *groupConstraints = [[NSMutableArray alloc] init];
    //水平约束
    [groupConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[labSavePwd]-5-[swtSave]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(labSavePwd,swtSave,labAuto,swtAuto)]];
    
    //-[labAuto(==60)]-5-[swtAuto]-20-|
    [groupConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[labAuto]-5-[swtAuto]-20-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(labAuto,swtAuto)]];
    
    //垂直约束
    [groupConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[btnLogin]-15-[labSavePwd(==35)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnLogin,labSavePwd)]];
    
    [groupConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[btnLogin]-15-[labAuto(==35)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnLogin,labAuto)]];
    
    [cView addConstraints:groupConstraints];
    groupConstraints = nil;
    
#pragma mark 找回密码约束
    
    NSMutableArray *lostConstraints = [[NSMutableArray alloc] init];
    //水平约束
    [lostConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[btnLostPwd(==60)]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(btnLostPwd)]];
    
    //垂直约束
    [lostConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[labSavePwd]-15-[btnLostPwd(==35)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labSavePwd,btnLostPwd)]];
    
    
    [cView addConstraints:lostConstraints];
    
    lostConstraints = nil;
    
    [self.btnLogin addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    
    //    _logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_logo.png"]];
    //    _logoImageView.frame = CGRectMake(([[UIScreen mainScreen] bounds].size.width - 69) / 2, 30, 69, 69);
    //    [self addSubview:_logoImageView];
    //
    //    _titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_title.png"]];
    //    _titleImageView.frame = CGRectMake(([[UIScreen mainScreen] bounds].size.width - 185) / 2, 100, 185, 55);
    //    [self addSubview:_titleImageView];
    
    _settingImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_setting"]];
    _settingImageView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-50, 20, 30, 30);
    [self addSubview:_settingImageView];
    _settingImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    
    [_settingImageView addGestureRecognizer:singleTap];
}

#pragma mark - 点击设置图片
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {

}

#pragma mark - 获取view的controller
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

-(void)login:(UIButton *) btn{
    [self endEditing:YES];
    if([self.delegate respondsToSelector:@selector(doLogin:)])
    {
        [self.delegate doLogin:self];
    }
}


@end
